using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Marlimer.Common;
using Marlimer.Common.Entidades;

namespace Marlimer.Data.Almacen
{
    public class alm_SubLinea
    {
        public DataTable getDTSubLinea(int ccod_lin)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsdtSubLinea";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();
            sqlcmd.Parameters.Add("@ccod_lin", SqlDbType.Int);
 
            sqlcmd.Parameters["@ccod_lin"].Value = ccod_lin; 
            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0];
        }

        public DataTable consultaSubLinea(String cdes_sublin, int ccod_lin, int flg_anulados)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsConsultaSubLinea";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@ccod_lin", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cdes_sublin", SqlDbType.NVarChar, 30);
            sqlcmd.Parameters.Add("@flg_anulados", SqlDbType.Int);

            sqlcmd.Parameters["@ccod_lin"].Value = ccod_lin;
            sqlcmd.Parameters["@cdes_sublin"].Value = cdes_sublin;
            sqlcmd.Parameters["@flg_anulados"].Value = flg_anulados;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0];
        }

        public resulMsgError guardar(InfoSubLinea sublin)
        {
            resulMsgError resul = new resulMsgError(false, "");

            SqlCommand sqlcmd = new SqlCommand();
            sqlcmd.CommandText = "spiaSubLinea";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@ccod_sublin", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cdes_sublin", SqlDbType.NVarChar, 30);
            sqlcmd.Parameters.Add("@ccod_lin", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cstatus", SqlDbType.NChar, 1);
            sqlcmd.Parameters.Add("@cmodo", SqlDbType.Int);

            sqlcmd.Parameters["@ccod_sublin"].Value = sublin.ccod_sublin;
            sqlcmd.Parameters["@cdes_sublin"].Value = sublin.cdes_sublin;
            sqlcmd.Parameters["@ccod_lin"].Value = sublin.ccod_lin;
            sqlcmd.Parameters["@cstatus"].Value = sublin.cstatus;
            sqlcmd.Parameters["@cmodo"].Value = sublin.cmodo;

            try
            {
                sqlcmd.ExecuteNonQuery();
                //verificamos el tipo de mensaje a mostrar
                if (sublin.cmodo == 3)
                {
                    resul.message = "Anulado satisfactoriamente";
                }
                else
                {
                    resul.message = "Se guardo satisfactoriamente";
                }
                resul.resul = true;
            }
            catch (Exception ex)
            {
                resul.message = ex.Message;
            }
            return resul;
        }

    }
}
